<?php 

class OAuth {
  
  /**
    *  Idenfitier of the client's credentials.  Think: username.
    *
    *  @var string
    */
  private $consumer_key_;
    
  /**
   *  Associates the request with the resource owner.
   *
   *  @var string
   */
  private $token_;
  
  /**
   *  If nonce exists, this is the method used to generate the nonce.
   *  Otherwise, if nocne was omitted this defaults to 'PLAINTEXT'.
   *
   *  @var string
   */
  private $signature_method_ = 'PLAINTEXT';
  
  /**
   *  Timestamp of the request; used in conjunction with the nonce
   *  to uniquely identify each request.
   *
   *  @var int
   */
  private $timestamp_;
  
  /**
   *  Nonce -> Number used once
   *  Nothing more than a uniquely generated string (doesn't have to be
   *  a number).  Used to identify each request and ensures each request
   *  is unique and has not been made before.  Generated by the client.
   *
   *  @var string
   */
  private $nonce_;
  
  /**
   *  An array containing all oauth fieldd we're concerning ourselves with.
   *
   *  $var array
   */
  private $desired_fields_ = array (
    'oauth_consumer_key',
    'oauth_token',
    'oauth_signature_method',
    'oauth_timestamp',
    'oauth_nonce'
  );
  
  public function __construct ()
  {
    
  }
  
  public function parse_fields ($fields)
  {
    if ( ! is_array ($fields))
      throw new Exception ('OAuth::parse_fields expects an array.');
      
    foreach ($fields as $field)
    {
      if (substr ($field, 0, 6) !== 'oauth_')
        throw new Exception ('All fields must begin with oauth_'); // All OAuth fields begin with oauth_
      
      list ($key, $value) = explode ('=', $field);
      
      if (empty ($key) || empty ($value))
        throw new Exception ('All fields should be in the format: key=value');

      if ( ! in_array ($key, $this->desired_fields_))
        continue; // We're not interested in this field.

      // Valid and desired field name
      $key = substr ($key, 6);      // Strip the oauth_ from the key name
      $var_name = "{$key}_";        // Set up our internal property name
      $this->$var_name = $value;    // And save it
    }
  }
  
}